<template>
  <section>
    <div class="model list">
      <img :src="IMGBASE64.XIAOCHEXIANLU2X" class="pink">
      <span class="title">OA功能模块</span>
      <div class="kong"></div>
      <div class="form2">
        <ul v-if="powerlist.length !== 0">
          <li v-for="(item, index) in powerlist">
            <el-checkbox :indeterminate="item.isIndeterminate" v-model="item.checkAll" @change="handleCheckAllChange(item, index, $event)">{{item.title}}</el-checkbox>
              <div style="margin: 15px 0;"></div>
              <el-checkbox-group v-model="item.checkedCities" @change="handleCheckedCitiesChange(item, index, $event)">
                <el-checkbox v-for="(city, id) in item.cities" :label="city" :key="id" :value="city.id">{{city}}</el-checkbox>
              </el-checkbox-group>
          </li>
        </ul>
      </div>
      <div class="bottom">
        <div class="right">
          <button class="btn btn-danger" @click="subLL" size="small">确定</button>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
// const cityOptions = ['上海', '北京', '广州', '深圳']
export default {
  data () {
    return {
      typeSelect: null,
      typeOption: [],
      powerlist: [],
      player: '凹凸鳗',
      upShow: false,
      Enline: null,
      teaName: null,
      line: null,
      lineOption: [],
      tableData: [{
        applyerId: '1',
        approvalTypestr: '凹凸鳗',
        applyer: 'kami'
      }]
    }
  },
  methods: {
    subLL () {
      let idList = []
      idList = []
      for (var i = 0; i < this.powerlist.length; i++) {
        for (var a = 0; a < this.powerlist[i].checkedId.length; a++) {
          idList.push({id: this.powerlist[i].checkedId[a]})
        }
      }
      // let _cookie = document.cookie.split(';')
      // let _cookieArray = []
      // _cookie.forEach(v => {
      //   let _d = v.split('=')
      //   _cookieArray.push({
      //     key: _d[0],
      //     value: _d[1]
      //   })
      // })
      this.$http.post(this._httpUrl + 'schoolConf/position/manage/resourceConf', {
        positionId: parseInt(this.$route.query.positionId),
        resourceChannel: 'PC',
        positionList: idList
      }, {'headers': {'yxt-xtoken': localStorage.xtoken}}).then(function (response) {
        if (response.body.code === '000') {
          this.$router.push('/schoolConf/position/manage')
        } else if (response.body.code === '887') {
          this.$router.push('/')
        } else if (response.body.code === '889') {
          this.$router.push('/')
        }
      })
    },
    upshow () {
      this.upShow = !this.upShow
    },
    append () {
      // store.append({ code: code++, label: 'testtest', children: [] }, data)
      this.upShow = true
    },
    nameSearch () {},
    handleCheckAllChange (item, index, e) {
      item.isIndeterminate = false
      if (item.checkAll === true && item.isIndeterminate === false) {
        item.checkedCities = item.cities
        item.checkedId.push(item.id)
        for (var i = 0; i < item.subList.length; i++) {
          item.checkedId.push(item.subList[i].id)
        }
      } else {
        item.checkedCities = []
        item.checkedId = []
      }
      this.$set(this.powerlist, index, item)
    },
    handleCheckedCitiesChange (item, index, e) {
      let checkedCount = item.checkedCities.length
      item.checkAll = checkedCount === item.cities.length
      item.isIndeterminate = checkedCount > 0 && checkedCount < item.cities.length
      if (item.checkedId.indexOf(item.id) < 0) {
        item.checkedId.push(item.id)
      }
      for (var i = 0; i < item.Acities.length; i++) {
        if (item.checkedCities.indexOf(item.Acities[i].name) >= 0 && item.checkedId.indexOf(item.Acities[i].id) < 0) {
          item.checkedId.push(item.Acities[i].id)
        }
      }
      this.$set(this.powerlist, index, item)
    }
  },
  mounted () {
    // let _cookie = document.cookie.split(';')
    // let _cookieArray = []
    // _cookie.forEach(v => {
    //   let _d = v.split('=')
    //   _cookieArray.push({
    //     key: _d[0],
    //     value: _d[1]
    //   })
    // })
    this.$http.post(this._httpUrl + 'schoolConf/position/manage/oaResource', {
      'positionId': this.$route.query.positionId
    }, {'headers': {'yxt-xtoken': localStorage.xtoken}}).then(function (response) {
      if (response.body.code === '000') {
        this.powerlist = response.data.data.schoolList
        let checkList = response.data.data.positionList
        // console.log(JSON.stringify(response.data.data.schoolList))
        for (var i = 0; i < this.powerlist.length; i++) {
          this.powerlist[i].title = this.powerlist[i].resourceName
          // this.powerlist[i].checkAll = false
          // this.powerlist[i].isIndeterminate = false
          let adsf = []
          let qwer = []
          for (var a = 0; a < this.powerlist[i].subList.length; a++) {
            adsf.push({
              name: this.powerlist[i].subList[a].resourceName,
              id: this.powerlist[i].subList[a].id
            })
            qwer.push(this.powerlist[i].subList[a].resourceName)
            // for (var b = 0; b < adsf.length; b++) {
            //   qwer[b] = adsf[b]
            // }
            this.powerlist[i].Acities = adsf
            this.powerlist[i].cities = qwer
          }
        }
        let schoolList = []
        for (var g = 0; g < this.powerlist.length; g++) {
          schoolList.push({
            title: this.powerlist[g].resourceName,
            Acities: this.powerlist[g].Acities,
            cities: this.powerlist[g].cities,
            resourceId: this.powerlist[g].resourceId,
            resourceName: this.powerlist[g].resourceName,
            subList: this.powerlist[g].subList,
            checkedCities: [],
            checkedId: [],
            checkAll: false,
            isIndeterminate: false
          })
        }
        // console.log(schoolList)
        // console.log(checkList)
        if (response.data.data.positionList != null) {
          for (var ir = 0; ir < schoolList.length; ir++) {
            for (var ar = 0; ar < checkList.length; ar++) {
              if (schoolList[ir].resourceId === checkList[ar].resourceId) {
                checkList[ar].title = schoolList[ir].title
                checkList[ar].Acities = schoolList[ir].Acities
                checkList[ar].cities = schoolList[ir].cities
                checkList[ar].checkedCities = []
                checkList[ar].checkedId = []
                checkList[ar].checkedId.push(checkList[ar].resourceId)
                if (checkList[ar].subList.length === schoolList[ir].subList.length) {
                  checkList[ar].checkAll = true
                  checkList[ar].isIndeterminate = false
                } else if (checkList[ar].subList.length === null) {
                  checkList[ar].checkAll = false
                  checkList[ar].isIndeterminate = false
                } else {
                  checkList[ar].checkAll = false
                  checkList[ar].isIndeterminate = true
                }
                for (var h = 0; h < checkList[ar].subList.length; h++) {
                  checkList[ar].checkedId.push(checkList[ar].subList[h].resourceId)
                  checkList[ar].checkedCities.push(checkList[ar].subList[h].resourceName)
                }
                schoolList[ir] = checkList[ar]
              }
            }
          }
          console.log(checkList)
          console.log(schoolList)
          this.powerlist = schoolList
        } else {
          for (var l = 0; l < this.powerlist.length; l++) {
            this.powerlist[l].checkedCities = []
            this.powerlist[l].checkedId = []
            this.powerlist[l].checkAll = false
            this.powerlist[l].isIndeterminate = false
          }
        }
      } else if (response.body.code === '887') {
        this.$router.push('/')
      } else if (response.body.code === '889') {
        this.$router.push('/')
      }
    })
  }
}
</script>

<style lang="less" scoped>
@import '/static/css/all-use.css';
@import '/static/css/material-dashboard.css';
  .retrieval{
    .body{
      span{
        margin-left: 4%;
      }
      span:first-child{
        margin-left: 0;
      }
      .firstSelect{
        width: 15%;
        border: 0;
        border-bottom: 1px solid #D7D7D7;
      }
      .select{
        margin-right: 4%;
        width: 10%;
        border: 0;
        border-bottom: 1px solid #D7D7D7;
      }
      input{
        margin-left: 4%;
        border: 0;
        border-bottom: 1px solid #D7D7D7;
      }
    }
  }
</style>

<style scoped>
  div[data-v-22bf8d34] ul
  {
    position: relative;
    left: 12%;
  }
  div[data-v-22bf8d34]
  {
    padding-bottom: 10px;
  }
  .el-upload--picture-card
  {
    margin-left: 9%; 
    margin-top: 40px;
    width: 80px; height: 80px;
  }
  .el-upload--picture-card i
  {
    position: relative; top: -28px;
  }
  .tree{
    position: relative; left: 60%;
    width: 40%;
  }
  .span{
    opacity: 0;
  }
  .form2{
    margin-top: -20px;
    margin-left: 8.3%;
    margin-right: 10px;
    overflow: hidden;
  }
  .form2 ul li{
    width: 96% !important;
    margin-right: 4% !important;
    margin-bottom: 3% !important;
  }
  .form2 .firstLine{
    padding: 30px 0 10px 0;
  }
  .form2 .secondLine{
    padding: 30px 0 50px 0;
  }
  .form2 .form-longText{
    width: 50%;
    border: 0;
    border-bottom: 1px solid #D7D7D7;
  }
  .form2 .form-text{
    width: 70%;
    border: 0;
    border-bottom: 1px solid #D7D7D7;
  }
  .form2 ul{
    width: 100%;
    margin-top: 30px;
    margin-left: -100px;
    overflow: hidden;
  }
  .form2 ul li .el-checkbox{
    position: relative; left: 14%;
  }
  .form2 ul li .el-checkbox-group{
    width: 100%;
  }
  .form2 ul li .el-checkbox-group .el-checkbox{
    margin-left: 4.7%;
    margin-right: 1%;
    margin-top: 8px;
  }
  .bottom .right{
    text-align: right;
    padding: 0 60px 0 0;
  }
  .el-checkbox-group{
    width: 100px;
  }
  .el-checkbox-group .el-checkbox{
    clear: both;
  }
  label[data-v-d525f0a6]{
    clear: both;
  }
</style>
